<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Word Book</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
    <div id="app">
        <h2>Add Word</h2>
        <form @submit.prevent="addWord">
            <div>
                <label for="word">Word:</label>
                <input type="text" v-model="word" required />
            </div>
            <div>
                <label for="attr">Part of Speech:</label>
                <input type="text" v-model="attr" required />
            </div>
            <div>
                <label for="meanings">Meanings:</label>
                <input type="text" v-model="meanings" required />
            </div>
            <div>
                <label for="synonyms">Synonyms:</label>
                <input type="text" v-model="synonyms" required />
            </div>
            <div>
                <label for="difficulty">Difficulty:</label>
                <input type="number" v-model="difficulty" required />
            </div>
            <button type="submit">Add Word</button>
        </form>

        <h2>Word List</h2>
        <ul>
            <li v-for="word in words" :key="word.id">
                {{ word.word }} - {{ Array.isArray(word.attr) ? word.attr.join(', ') : '' }} - 
                {{ Array.isArray(word.meanings) ? word.meanings.join(', ') : '' }} - 
                {{ Array.isArray(word.synonyms) ? word.synonyms.join(', ') : '' }} - 
                Difficulty: {{ word.difficulty }}
            </li>
        </ul>
    </div>

    <script>
        new Vue({
            el: '#app',
            data: {
                word: '',
                attr: '',
                meanings: '',
                synonyms: '',
                difficulty: 1,
                words: []
            },
            created() {
                this.fetchWords();
            },
            methods: {
                async addWord() {
                    const newWord = {
                        word: this.word,
                        attr: this.attr.split(',').map(item => item.trim()),
                        meanings: this.meanings.split(',').map(item => item.trim()),
                        synonyms: this.synonyms.split(',').map(item => item.trim()),
                        difficulty: this.difficulty
                    };
                    try {
                        await axios.post('http://localhost:3000/add-word', newWord);
                        this.fetchWords();  // Refresh the word list
                        this.word = '';
                        this.attr = '';
                        this.meanings = '';
                        this.synonyms = '';
                        this.difficulty = 1;
                    } catch (error) {
                        console.error('Error adding word:', error);
                    }
                },
                async fetchWords() {
                    try {
                        const response = await axios.get('http://localhost:3000/words');
                        this.words = response.data;
                    } catch (error) {
                        console.error('Error fetching words:', error);
                    }
                }
            }
        });
    </script>
</body>
</html>
